/*
 * File:   Camera.cpp
 * Author: Elevator
 *
 * Created on 30 Март 2013 г., 14:07
 */

#include <math.h>
#include "Camera.h"

using namespace core;

const float Camera::SPEED = 5.0f;

Camera::Camera( ) :
	_pos( Transform::ZERO ),
	_vel( Transform::ZERO ),
	_acc(Vec2::ZERO),
	_scale( 1.0f ),
	_velFactor( 0.1f ),
	_scaleFactor( 1.0f )
{
}

Camera::Camera( const Transform& pos, const Transform& vel, float scale ) :
	_pos( pos ),
	_vel( vel ),
	_acc(Vec2::ZERO),
	_scale( scale ),
	_velFactor( 0.1f ),
	_scaleFactor( 1.0f )
{
}

Camera::Camera( const Camera& camera ) :
	_pos( camera.GetPos( ) ),
	_vel( camera.GetVel( ) ),
	_acc(camera._acc),
	_scale( camera.GetScale( ) ),
	_velFactor( camera._velFactor ),
	_scaleFactor( camera._scaleFactor )
{
}

Camera::~Camera( )
{
}

void Camera::Update( float dt )
{
	_vel.Vec += _acc * dt;
	_vel *= powf( _velFactor, dt );

	float len = _vel.Vec.Length( );
	if( len > SPEED / _scale )
		_vel.Vec = _vel.Vec.Normalize( ) * SPEED / _scale;
	if( len < 0.01f )
		_vel.Vec = Vec2::ZERO;

	_pos += _vel * dt;
	_scale *= powf( _scaleFactor, dt );

}

void Camera::Accelerate( Vec2 direction )
{
	_acc += direction * SPEED / _scale;
}

void Camera::KeyPressed( KeyboardKey key )
{
	float scaleFactor = _scaleFactor;

	switch( key )
	{
	case KeyboardKey::LEFT:
		Accelerate( Vec2::LEFT );
		return;
	case KeyboardKey::RIGHT:
		Accelerate( Vec2::RIGHT );
		return;
	case KeyboardKey::UP:
		Accelerate( Vec2::UP );
		return;
	case KeyboardKey::DOWN:
		Accelerate( Vec2::DOWN );
		return;
	case KeyboardKey::KP_ADD:
		_scaleFactor = scaleFactor + 0.1f * SPEED;
		return;
	case KeyboardKey::KP_SUBTRACT:
		_scaleFactor = scaleFactor - 0.1f * SPEED;
		return;
	default:
		return;
	}
}

void Camera::KeyReleased( KeyboardKey key )
{
	float scaleFactor = _scaleFactor;

	switch( key )
	{
	case KeyboardKey::LEFT:
		Accelerate( Vec2::RIGHT );
		return;
	case KeyboardKey::RIGHT:
		Accelerate( Vec2::LEFT );
		return;
	case KeyboardKey::UP:
		Accelerate( Vec2::DOWN );
		return;
	case KeyboardKey::DOWN:
		Accelerate( Vec2::UP );
		return;
	case KeyboardKey::KP_ADD:
		_scaleFactor = scaleFactor - 0.1f * SPEED;
		return;
	case KeyboardKey::KP_SUBTRACT:
		_scaleFactor = scaleFactor + 0.1f * SPEED;
		return;
	default:
		return;
	}
}
